#include "stdafx.h"
#include "CommonHelp.h"
#include <iostream>
#include <string>
#include <algorithm>
namespace CommonHelp {
    static std::string getFilenameFromUrlComplete(const std::string& url) {
        if (url.empty()) return "";

        std::string processedUrl = url;

        // 处理以'/'结尾的URL
        if (processedUrl.back() == '/') {
            processedUrl.pop_back();
        }

        // 查找最后一个'/'
        size_t lastSlash = processedUrl.find_last_of('/');
        if (lastSlash == std::string::npos) {
            // 如果没有'/'，直接返回整个URL（可能是相对路径）
            processedUrl = url;
        }
        else {
            processedUrl = processedUrl.substr(lastSlash + 1);
        }

        // 移除查询参数
        size_t queryPos = processedUrl.find('?');
        if (queryPos != std::string::npos) {
            processedUrl = processedUrl.substr(0, queryPos);
        }

        // 移除片段标识
        size_t fragmentPos = processedUrl.find('#');
        if (fragmentPos != std::string::npos) {
            processedUrl = processedUrl.substr(0, fragmentPos);
        }

        return processedUrl.empty() ? "index.html" : processedUrl;
    }
}